#include "dkd_convert.h"
#include "ui_dkd_convert.h"

int index[28];
int row1;

dkd_convert::dkd_convert(QWidget *parent) :
    QDialog(parent)
{
    setupUi(this);

    connect(browserButton,SIGNAL(clicked()),this,SLOT(setSrcEdit()));
    connect(SrcEdit,SIGNAL(textChanged(QString)),this,SLOT(setOkEndable(QString)));
    connect(AdvanceBox,SIGNAL(stateChanged(int)),this,SLOT(setpath(int)));
    connect(lineEdit,SIGNAL(textChanged(QString)),this,SLOT(findtext(QString)));

    tableWidget->setStyleSheet("background-color:rgba(200,255,255,255)");
    tableWidget->setStyleSheet("selection-background-color: blue");
    SrcEdit->setText("dicts/tesv.csv");
    advancedwidget->setHidden(1);
    Cantfind->setHidden(1);
    //BatchButton->setHidden(1);
}

bool dkd_convert::findtext(QString text)
{
    char *a=text.left(1).toUpper().toLatin1().data();
    int len=text.length();
    if(len==0)
    {
        return hidden(1);
    }
    QString str;
    if(*a<='Z'&&*a>='A')
    {
        int n=*a-'A'+1;
        for(int i=index[n];i<index[n+1];i++)
        {
            str=tableWidget->item(i,0)->text().left(len);
            if(QString::compare(text,str,Qt::CaseInsensitive)==0)
            {
                tableWidget->setCurrentCell(i,QAbstractItemView::SelectRows);
                return hidden(1);
            }
        }
        return hidden(0);
    }
    else
    {
        for(int i=0;i<index[1];i++)
        {
            str=tableWidget->item(i,0)->text().left(len);
            if(QString::compare(text,str,Qt::CaseInsensitive)==0)
            {
                tableWidget->setCurrentCell(i,QAbstractItemView::SelectRows);
                return hidden(1);
            }
        }
        for(int i=0;i<row1;i++)
        {
            str=tableWidget->item(i,1)->text().left(len);
            if(QString::compare(text,str)==0)
            {
                tableWidget->setCurrentCell(i,QAbstractItemView::SelectRows);
                return hidden(1);
            }
        }
        return hidden(0);
    }

}

bool dkd_convert::hidden(bool i)
{
    tableWidget->setHidden(!i);
    Cantfind->setHidden(i);
    return i;
}



bool dkd_convert::setSrcEdit()
{
    QString str=QFileDialog::getOpenFileName(this,tr("Open File"),".",tr("CSV(*.csv);;Dictionary(*.esd)"));
    SrcEdit->setText(str);
    return 1;
}

bool dkd_convert::setpath(int x)
{
    bool i=!(bool)x;
    advancedwidget->setHidden(i);
    return i;
}

bool dkd_convert::setOkEndable(QString str)
{
    if(QString::compare(str.right(4),".csv",Qt::CaseInsensitive)!=0)
        return 0;
    QFileInfo file(str);
    if(file.exists())
        return setTable(str);
    else
        return 0;
}

bool dkd_convert::setTable(QString file)
{
    row1=0;
    QString line;
    QStringList list;
    tableWidget->clearContents();
    tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    tableWidget->setColumnCount(3);
    tableWidget->setHorizontalHeaderLabels(QStringList()<<tr("English")<<tr("Chinese")<<tr("Property"));
    tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
    tableresize();
    QFile File(file);
    File.open(QIODevice::ReadOnly);
    if(QString::compare(file.right(4),".csv",Qt::CaseInsensitive)==0)
    {
        while(!File.atEnd())
        {
            tableWidget->insertRow(row1);
            line=File.readLine();
            list=line.split(",");
            for (int i=0;i<3;i++)
                tableWidget->setItem(row1,i,new QTableWidgetItem(list.at(i)));
            ++row1;
        }
    }
    File.close();
    tableWidget->sortByColumn(0,Qt::AscendingOrder);
    tableWidget->verticalHeader()->setResizeMode(QHeaderView::Fixed);
    tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    makeindex();
    return 1;
}

bool dkd_convert::makeindex()
{
    int i=0;
    char *ch;
    char alpha='A';
    memset(index+1,-1,26*sizeof(int));
    index[0]=0;
    index[27]=row1;
    for(i=0;i<row1&&alpha<='Z';i++)
    {
        ch=tableWidget->item(i,0)->text().left(1).toUpper().toLatin1().data();
        if(*ch>=alpha)
        {
            alpha=*ch;
            index[*ch-'A'+1]=i;
            ++alpha;
        }
    }
    return 0;
}

bool dkd_convert::tableresize()
{
    int WUnit=tableWidget->width()/5-12;
    tableWidget->verticalHeader()->setFixedWidth(40);
    tableWidget->setColumnWidth(0,2*WUnit);
    tableWidget->setColumnWidth(1,2*WUnit);
    tableWidget->setColumnWidth(2,WUnit);
    return 1;
}

void dkd_convert::resizeEvent(QResizeEvent *event)
{
    tableresize();
}


dkd_convert::~dkd_convert()
{
    delete ui;
}
